//
//  SandTableFurnitureView.m
//  Model
//
//  Created by zeb-apple on 2017/6/7.
//  Copyright © 2017年 YDJH. All rights reserved.
//

#import "SandTableFurnitureView.h"
#import "HZFurniture.h"
#import "HZFurniture_Header.h"
#import "HZRoomLogic.h"

@interface SandTableFurnitureView ()

@property (nonatomic, strong) UIImageView *furnitureBtn; // 对家具操作的按钮 根据状态显示不同图片

@end
@implementation SandTableFurnitureView


- (instancetype)init {
    self = [super init];
    if (self) {
        self.userInteractionEnabled = YES;
        //  self.backgroundColor = [UIColor redColor];
        [self initView];
    }
    return self;
}
- (void)initView {
    
    [self addSubview:self.furnitureBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(furnitureBtnclick:)];
    [self.furnitureBtn addGestureRecognizer:tap];
    
}

//点击道具操作按钮
- (void)furnitureBtnclick:(UITapGestureRecognizer *)recognizer {
    
    self.furnitureClickBlock(self.furniture, (UIImageView *)[recognizer view]);
    
}

- (void)setFurniture:(HZFurniture *)furniture {
    _furniture = furniture;
    
    [self updataself];
    
    [self updatafurnitureBtn];
    
}
// 对道具载体进行更新
- (void)updataself {
    
    UIImage *furniturePicture = [UIImage imageNamed:self.furniture.furniturePicture];
    self.frame = CGRectMake(0, 0, furniturePicture.size.width, furniturePicture.size.height);
    self.image = furniturePicture;
    [self adjustPointWithFurniture:self.furniture];
}
// 对操作按钮进行更新
- (void)updatafurnitureBtn {
    
    //根据家具类型添加不同图片
    UIImage *image = [HZRoomLogic goBackImageWithFurnitureState:self.furniture.furnitureState];
    self.furnitureBtn.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    self.furnitureBtn.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
    self.furnitureBtn.image = image;
}
- (UIImageView *)furnitureBtn {
    if (!_furnitureBtn) {
        _furnitureBtn = [[UIImageView alloc] init];
        _furnitureBtn.userInteractionEnabled = YES;
        _furnitureBtn.alpha = 0.7;
    }
    return _furnitureBtn;
}
#pragma mark
#pragma mark logic
- (void)adjustPointWithFurniture:(HZFurniture *)furniture {
    
    if ([self.furniture.furnitureName rangeOfString:Staircase_Type].location != NSNotFound) { //梯子
        NSArray *positionxy = [self.furniture.position componentsSeparatedByString:@","];
        self.center = CGPointMake([[positionxy firstObject] floatValue], [[positionxy lastObject] floatValue]);
    }else if ([self.furniture.furnitureName rangeOfString:Window_Type].location != NSNotFound){
        NSArray *positionxy = [self.furniture.position componentsSeparatedByString:@","];
        self.center = CGPointMake([[positionxy firstObject] floatValue], [[positionxy lastObject] floatValue]);
    }else if ([self.furniture.furnitureName rangeOfString:Door_Type].location != NSNotFound){
        NSArray *positionxy = [self.furniture.position componentsSeparatedByString:@","];
        self.center = CGPointMake([[positionxy firstObject] floatValue], [[positionxy lastObject] floatValue]);
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
